#!/bin/bash

_help()
{
  echo '---该shell脚本用于一键安装nginx+apache+php+mysql+memcache+mongodb+负载均
衡高可用架构---'
  echo '参数说明：'
  echo '-w :web服务器节点'
  echo '-lb[12]:负载均衡节点 1，2为节点选项；1:mongodb 监控节点 '
  echo '-db[12]:数据库节点节点 1，2为节点选项；1：mongodb '
  echo '-help 帮助'
}
#配置haproxy+keepalived 为webserver做高可用负载均衡
Set_hap_keep()
{
#安装并配置haproxy
 sudo apt-get update
 sudo apt-get install haproxy
 echo 'please input the virtual ip for web-server'
 read vip
 sete1="listen webfarm $vip:80"
 sed -i "/listen/c$sete1" ./Hajq/haproxy/haproxy.cfg
 echo 'please input the number of web-server'
 read number
 temp=1
 if [ $number -ge 1 ]
 then
  while [ $temp -le $number ]
  do
    echo "please input the ip of web-server$temp"
    read ip
    echo "please input the weight of web-server$temp"
    read weight
    sete2="    server  web$temp $ip:80 check weight $weight"
    if [ $temp -eq 1 ]    #若为web1，则将原有server删除
    then 
      sed -i "/server/d" ./Hajq/haproxy/haproxy.cfg
      sed -i "32a\\$sete2" ./Hajq/haproxy/haproxy.cfg
    else
      temp1=$((temp-1))
      sed -i "/web$temp1/a\\$sete2" ./Hajq/haproxy/haproxy.cfg
    fi
    temp=$((temp+1))
   done
 else
   echo 'error:the number is illegal'
   exit 0
 fi 
 sudo cp ./Hajq/haproxy/haproxy.cfg /etc/haproxy/
 sudo rm /etc/sysctl.conf 
 sudo cp ./Hajq/etc/sysctl.conf /etc/
 sudo sysctl -p
 sudo rm /etc/default/haproxy
 sudo cp ./Hajq/haproxy/haproxy /etc/default/
 

#安装并配置keepalived
 sudo apt-get install keepalived
 #设置优先级
 if [ $1 -eq 1 ] 
 then
    sete3="    priority 101"
 else
    sete3="    priority 100"
 fi
 sed -i "/priority/c$sete3" ./Hajq/keepalived/keepalived.conf
 #设置虚拟ip
 _vip="        $vip"
 sed -i "/virtual_ipaddress/{n;d}" ./Hajq/keepalived/keepalived.conf
 sed -i "/virtual_ipaddress/a\\$_vip" ./Hajq/keepalived/keepalived.conf
 sudo rm /etc/keepalived/keepalived.conf #zhucong
 sudo cp ./Hajq/keepalived/keepalived.conf /etc/keepalived/
 
 if [ $1 -eq 1 ]
 then
 sudo ifconfig eth0:0 $vip netmask 255.255.252.0 up #zhu
 sudo service haproxy restart
 sudo service keepalived restart
 #sudo ifconfig netmask eth0:0 10.102.11.117
 fi
 
}
#配置heartbeat+ldirectord为mysql做高可用负载均衡
Set_heartb_ld()
{
 sudo modprobe ip_vs_dh
 sudo modprobe ip_vs_ftp
 sudo modprobe ip_vs
 sudo modprobe ip_vs_lblc
 sudo modprobe ip_vs_lblcr
 sudo modprobe ip_vs_lc
 sudo modprobe ip_vs_nq
 sudo modprobe ip_vs_rr
 sudo modprobe ip_vs_sed
 sudo modprobe ip_vs_sh
 sudo modprobe ip_vs_wlc
 sudo modprobe ip_vs_wrr
 sudo rm /etc/modules
 sudo cp ./etc/modules /etc/
 sudo sysctl -p
 sudo apt-get update
 sudo apt-get install heartbeat 
 sudo apt-get install ldirectord
 sudo apt-get install libdbi-perl
 sudo apt-get install libdbd-mysql-perl 
 sudo apt-get install  libmysqlclient15-dev
 #/usr/sbin/ldirectord -h测试组件安装，也可以用status
 echo 'please input the ip and the name of lb1 like: 0.0.0.0 host1'
 read host1
 echo 'please input the ip and the name of lb2 like: 0.0.0.0 host2'
 read host2

#配置hosts文件--
 set=$host1"\n"$host2
 echo $set
 sed -i "2a\\$set" /etc/hosts
#gai zhujiming
 if [ $1 -eq 1 ]
 then
  hostname=`echo $host1|awk '{print$2}'`
 else
  hostname=`echo $host2|awk '{print$2}'`
 fi
 sed -i "1c\\$hostname" /etc/hostname
#配置ha.cf文件
 n_node1="node `echo $host1|awk '{print$2}'`"
 n_node2="node `echo $host2|awk '{print$2}'`"
 n_node=$n_node1"\n"$n_node2

 sed -i "/node/d" ./Hajq/etc/ha.cf  #删除原有的node
 sed -i "4a\\$n_nod" ./Hajq/etc/ha.cf #添加新的node
 sudo cp ./Hajq/etc/ha.cf /etc/ha.d/ #

#配置haresources文件
 echo 'please input the vitual ip for database and the subnet mask like 127.0.0.1/22'
 read vip_db
 temp=`echo $host1|awk '{print$2}'`
 set="$temp lvs IPaddr::$vip_db/eth0:1 ldirectord::ldirectord.cf"
 sed -i "/lvs/d" ./Hajq/etc/haresources
 sed -i "1a\\$set" ./Hajq/etc/haresources
 sudo cp ./Hajq/etc/haresources /etc/ha.d/ #
 sudo cp ./Hajq/etc/authkeys /etc/ha.d/

#配置ldirectord.cf文件
 sete1="virtual=`echo $vip_db|cut -d/ -f1`:3306"
 sed -i "/virtual/d" ./Hajq/etc/ldirectord.cf
 sed -i "6a\\$sete1" ./Hajq/etc/ldirectord.cf

 echo 'please input the real ip of database-server1'
 read re_ip1
 echo 'please input the real ip of database-server2'
 read re_ip2
 set1="     real = $re_ip1::3306 gate"
 set2="     real = $re_ip2::3306 gate"
 sete2=$set1"\n"$set2
 sed -i "/real/d" ./Hajq/etc/ldirectord.cf
 sed -i "8a\\$sete2" ./Hajq/etc/ldirectord.cf
 sudo cp ./Hajq/etc/ldirectord.cf /etc/ha.d/ #
 sudo chmod 600 /etc/ha.d/authkeys
 sete3="VIP=`echo $vip_db|cut -d/ -f1`"
 #sed  "/VIP=/c\\$sete3" ./lvs
 sed -i  "/VIP=/c\\$sete3" ./Hajq/etc/lvs
 sudo cp ./Hajq/etc/lvs /etc/init.d/
 sudo chmod 755 /etc/init.d/lvs  #这两个改权限的操作非常重要，如果不改的话，将无法执行
 sudo update-rc.d -f ldirectord remove
 sudo update-rc.d -f heartbeat remove
 sudo update-rc.d heartbeat start 90 2 3 4 5 . stop 05 0 1 6 .
 
}
################负载均衡节点##############
_Lb() 
{ 
 if [ $1 -eq 1 ]
 then 
  #设置haproxy+keepalived web 负载均衡
  Set_hap_keep 1
  #设置heartbeat+ldirectord db 负载均衡
  Set_heartb_ld 1
  #mongodb 副本集 中的监控节点
  sudo apt-get install mongodb
  sudo mongod --dbpath /var/lib/mongodb --logpath /var/log/mongodb.log --fork --replSet shop
 else
  #设置haproxy+keepalived web 负载均衡
  Set_hap_keep 2
  #设置heartbeat+ldirectord db 负载均衡
  Set_heartb_ld 2
 fi
 echo 'please reboot'
}
#安装mysql，并配置集群
Set_mysql()
{
  sudo apt-get install mysql-server mysql-client
  rm /etc/mysql/my.cnf   #修改配置文件
  if [ $1 -eq 1 ] 
  then
   cp ./mysql_setup/my1.cnf /etc/mysql/my.cnf
   sudo service mysql restart
  else
   cp ./mysql_setup/my2.cnf /etc/mysql/my.cnf
   sudo service mysql restart
  fi   
  #进入数据库修改配置
  export mysqlbinpath="/usr/bin" #mysql路径
#variables for master
  export master_mysql_root_passwd="123456"  #mysql主数据库的root密码
  export replication_user="root"  #用于复制的mysql用户
  export replication_passwd="123456" #mysql用户copydb的密码
  export replication_db="shop" #需要同步的数据库名
# export master_ip="127.0.0.1"    #mysql主服务器IP
  export slave_ip=""
  echo 'input slave_ip'
  read slave_ip
  mysql -uroot -p${master_mysql_root_passwd} -e "use mysql;GRANT ALL PRIVILEGES ON *.* TO'root'@'%'IDENTIFIED BY'123456' WITH GRANT OPTION;flush privileges;CREATE DATABASE ${replication_db};stop slave;CHANGE MASTER TO MASTER_HOST=\"${slave_ip}\",MASTER_USER=\"${replication_user}\",MASTER_PASSWORD=\"${replication_passwd}\",MASTER_PORT=3306;start slave;select sleep(3);show slave status\G"

}
#安装mongodb并设置副本集
Set_mongodb()
{
  sudo apt-get install mongodb
  sudo rm /etc/mongodb.conf
  sudo cp ./dbjq/mongodb/mongodb.conf /etc/
  sudo mongod -f /etc/mongod.conf
  sudo service mongodb stop
  sudo kill -s 9 `pgrep mongod`
  sudo rm /var/lib/mongodb/mongod.lock
  sudo mongod --dbpath /var/lib/mongodb --logpath /var/log/mongo.log   --fork  --replSet shop
 #修改ko.js文件
 if [ $1 -eq 1 ]
 then
  echo 'please input the ip of the master'
  read ip_master
  echo 'please input the ip of the backup'
  read ip_backup
  echo 'please input the ip of the arbiter'
  read ip_arbiter
  ip_master="                    \"host\":\"$ip_master:27017\""
  ip_backup="                    \"host\":\"$ip_backup:27017\""
  ip_aritber="                   \"host\":\"$ip_arbiter:27017\",\"arbiterOnly\":true"
  sed -i "/:1,/{n;d}" ./dbjq/ko.js
  sed -i "/:1,/a\\$ip_master" ./dbjq/ko.js
  sed -i "/:2,/{n;d}" ./dbjq/ko.js
  sed -i "/:2,/a\\$ip_backup" ./dbjq/ko.js
  sed -i "/:3,/{n;d}" ./dbjq/ko.js
  sed -i "/:3,/a\\$ip_aritber" ./dbjq/ko.js
  sudo mongo dbjq/ko.js  # 区别
  fi
}
#安装缓存
Set_memcached()
{
 sudo apt-get install memcached
 sudo rm /etc/memcached.conf
 sudo cp ./dbjq/memcache/memcached.conf /etc/
}
#设置虚拟ip 为了mysql集群
Set_vip()
{
sudo rm /etc/sysctl.conf
sudo cp ./dbjq/etc/sysctl.conf /etc/
sudo sysctl -p
echo 'please input the vitual ip for database-server'
read vip
set="address $vip"
sed -i "/address/c\ $set" ./dbjq/etc/interfaces
sudo rm /etc/network/interfaces
sudo cp ./dbjq/etc/interfaces /etc/network/
sudo ifdown eth0:1
sudo ifup eth0:1
}
##############db节点#################
_db()
{
  if [ $1 -eq 1 ]
  then
    Set_mysql 1
    Set_mongodb 1
  else
    Set_mysql 2
    Set_mongodb 2
  fi
  Set_memcached
  Set_vip
}
#安装apache2+nginx+mysql+php环境
Set_lanmp()
{
 sudo apt-get update
 sudo apt-get install apache2 mysql-server mysql-client php5 php5-gd php5-mysql
 sudo chmod 777 /var/www
 sudo chmod 777 -R /var/www
 sudo apt-get install phpmyadmin
 sudo a2enmod rewrite
 sudo service apache2 stop
 sudo rm /etc/apache2/apache2.conf
 sudo cp ./webjq/apache2/apache2.conf /etc/apache2/
 sudo rm /etc/apache2/sites-available/default
 sudo cp ./webjq/apache2/default /etc/apache2/sites-available/
 sudo rm /etc/apache2/ports.conf
 sudo cp ./webjq/apache2/ports.conf /etc/apache2/
 sudo apt-get install curl libcurl3 libcurl3-dev php5-curl
 sudo apt-get install php-pear php5-dev
 sudo pecl install mongo
 sudo cp ./webjq/mongodb/mongodb.ini /etc/php5/conf.d/mongodb.ini
 sudo apt-get install php5-memcache
 sudo cp ./webjq/memcache/memcache.ini /etc/php5/conf.d/memcache.ini
 sudo service apache2 restart
}
#安装shop模板
Install_shop()
{
 echo 'please input the ip of memcached-server'
 read mem_ip
 sete="\$mem->connect('$mem_ip',11211);"
 sed -i "/mem->connect/c$sete" ./webjq/shop4/framework/lib/db/db_mysql_class.php
 echo 'please input the ip of mongodb-master'
 read mon_ipm
 echo 'please input the ip of mongodb-backup'
 read mon_ipb
 sete1="'mongodb://$mon_ipm:27017,$mon_ipb:27017',"
 sed -i  "/mongodb:/c$sete1" ./webjq/shop4/protected/controllers/index.php
 
 sudo cp -R ./webjq/shop4 /var/www/
 sudo chmod 777 -R /var/www/shop4/
 sudo firefox http://127.0.0.1:8080/shop4/install
 sudo apt-get install nginx 
 sudo chmod 777 -R /usr/share/nginx/www/
 sudo apt-get install php5-cli php5-cgi mysql-server php5-mysql
 sudo apt-get install spawn-fcgi
 sudo spawn-fcgi -a 127.0.0.1 -p 9000 -C 10 -u www-data -f /usr/bin/php-cgi
 sudo rm /etc/nginx/sites-available/default
 sudo cp ./webjq/nginx/1/default /etc/nginx/sites-available/
 sudo service nginx restart
 sudo cp -R ./webjq/shop4 /usr/share/nginx/www/
 sudo chmod 777 -R /usr/share/nginx/www/shop4/
 sudo firefox http://127.0.0.1/shop4/install
 sudo service nginx stop
 sudo sudo rm /etc/nginx/sites-available/default
 sudo cp ./webjq/nginx/2/default /etc/nginx/sites-available/
 sudo service nginx restart
 sudo cp ./webjq/hachecker.php /var/www/
 sudo cp ./webjq/hachecker.php /usr/share/nginx/www/
}
#############web节点################
_web()
{
 Set_lanmp
 Install_shop
}

#############主程序################
if [ $UID -ne 0 ]    ##检查权限
then
   echo 'error:无权限！'
   exit 0
else
 if [ $# -ne 1 ]     ##检查参数
 then
   echo 'error:参数错误！' 
   _help
 else
   case $1 in
   -w) _web  ;;
   -lb1) _Lb 1 ;;
   -lb2) _Lb 2 ;;
   -db1) _db 1 ;;
   -db2) _db 2 ;;
   -help) _help ;;
   *) echo 'error:参数错误！'
      _help;;
   esac
 fi
fi
